package com.web.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.web.common.pojos.Car;
import com.web.common.responseResult.ResponseResult;
import com.web.common.vo.CompareInfoVo;
import com.web.mapper.BrandMapper;
import com.web.mapper.CarMapper;
import com.web.service.Task5Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/6/13 下午2:18]
 */
@Service
public class Task5ServiceImpl extends ServiceImpl<CarMapper, Car> implements Task5Service {
    @Autowired
    private BrandMapper brandMapper;
    @Override
    public ResponseResult<CompareInfoVo> getCompareInfo() {
        List<String> brandNameList = new ArrayList<>();
        brandNameList.add("比亚迪");
        brandNameList.add("特斯拉");
        int start = 201903,end = 202404;
        List<Integer> dates = brandMapper.selectDateByBrand(brandNameList.get(0),start,end);
        List<List<Integer>> dateValues = new ArrayList<>();
        List<Integer> brandValues = new ArrayList<>();
        for (String brandName : brandNameList) {
            List<Integer> brandSaleCounts = brandMapper.selectCountByDateAndBrand(brandName,start,end);
            int totalSum = brandSaleCounts.stream()
                    .mapToInt(Integer::intValue)
                    .sum();
            System.out.println(totalSum);
            brandValues.add(totalSum);
            dateValues.add(brandSaleCounts);
        }

        List<String> datesAsString = dates.stream()
                .map(date -> {
                    String year = String.format("%04d", date / 100);
                    String month = String.format("%02d", date % 100);
                    return year + "年" + month + "月";
                })
                .collect(Collectors.toList());
        CompareInfoVo compareInfoVo = new CompareInfoVo();
        compareInfoVo.setDates(datesAsString);
        compareInfoVo.setDateValues(dateValues);
        compareInfoVo.setBrandNames(brandNameList);
        compareInfoVo.setBrandValues(brandValues);
        return ResponseResult.okResult(compareInfoVo);
    }
}
